// MainDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ScreenShots.h"
#include "MainDlg.h"
#include "MainWindow.h"
#include "ScreenShotsDlg.h"
#include "PositionEditDlg.h"


// MainDlg dialog

IMPLEMENT_DYNAMIC(MainDlg, CDialog)

MainDlg::MainDlg(CWnd* pParent /*=NULL*/)
	: CDialog(MainDlg::IDD, pParent)
{

}

MainDlg::~MainDlg()
{
	for(int i=0; i<(int)Windows.size(); i++)
	{
		delete Windows[i].rec;
	}
}

void MainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TABLE_LIST, TableList);
}


BEGIN_MESSAGE_MAP(MainDlg, CDialog)
	ON_BN_CLICKED(IDC_BTN_ADD_TABLE, &MainDlg::OnBnClickedBtnAddTable)
	ON_BN_CLICKED(IDC_BTN_DEL_TABLE, &MainDlg::OnBnClickedBtnDelTable)
	ON_BN_CLICKED(IDC_BUTTON3, &MainDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON_EDIT, &MainDlg::OnBnClickedButtonEdit)
	ON_BN_CLICKED(IDC_BUTTON_START, &MainDlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &MainDlg::OnBnClickedButtonStop)
END_MESSAGE_MAP()

BOOL MainDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	DWORD style = ::SendMessage(TableList.m_hWnd, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
	style |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
	::SendMessage(TableList.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, style);


	TableList.InsertColumn(0, _T("#"), LVCFMT_CENTER);
	TableList.InsertColumn(1, _T("Players cnt"), LVCFMT_CENTER);
	TableList.InsertColumn(2, _T("Stack"), LVCFMT_CENTER);	
	TableList.InsertColumn(3, _T("Last message"), LVCFMT_CENTER);
	
	CRect r;
	TableList.GetClientRect(&r);
	int w =  r.right-r.left;
	TableList.SetColumnWidth (0, (int)w*0.3);
	for (int i=1; i < 3; i++)
		TableList.SetColumnWidth (i, 50);
	TableList.SetColumnWidth (3, (int)0.7*w - 100);

	ObserverManager &om = ObserverManager::GetInstance();
	om.Register(this);

	return TRUE;
}



void MainDlg::UpdateTable( HWND iHwnd, int iPlayersNum, int iStack, TCHAR *iDecision)
{
	//find window in the array
	int ind = -1;
	for(int i=0; i<(int)Windows.size(); i++)
	{
		if(Windows[i].h == iHwnd)
		{
			ind = i;
			break;
		}
	}


	//update list view
	if(ind >= 0)
	{
		CString buf;
		buf.Format(_T("%d"), iPlayersNum);
		TableList.SetItemText(ind, 1, buf);
		buf.Format(_T("%d"), iStack);
		TableList.SetItemText(ind, 2, buf);
		TableList.SetItemText(ind, 3, iDecision);
	}
}

// MainDlg message handlers


void MainDlg::OnBnClickedBtnAddTable()
{
	// TODO: Add your control notification handler code here
	ScreenShotsDlg dlg;
	if(dlg.DoModal() == IDOK && dlg.GetCapturedWindow() != 0)
	{
		PositionEditDlg posdlg;
		if(posdlg.DoModal() == IDOK)
		{
			//register receiver
 			DataReceiver *rec = new DataReceiver();
			WDG.AddReceiver(dlg.GetCapturedWindow(), rec);
			rec->Bot.SetWindow(dlg.GetCapturedWindow());
			rec->SetPos(posdlg.Position-1);
			rec->SetWindow(dlg.GetCapturedWindow());

			rec->SetAutoPlay(true);

			//add window
			Windows.push_back(Table(dlg.GetCapturedWindow(), posdlg.Position, rec));
		

			//add row to grid
			TCHAR caption[2000];
			::GetWindowText(dlg.GetCapturedWindow(), caption, 2000);
			int n = TableList.InsertItem(Windows.size()-1, _T("Item1"));
			TableList.SetItemText(n, 0, caption);
			TableList.SetItemText(n, 1, _T("Not set"));
			TableList.SetItemText(n, 2, _T("Not set"));
			TableList.SetItemText(n, 3, _T("Not set"));
		}
	}	
}

void MainDlg::OnBnClickedBtnDelTable()
{
	POSITION pos = TableList.GetFirstSelectedItemPosition();
	if (pos == NULL)
		return;

	while (pos)
	{
		int nItem = TableList.GetNextSelectedItem(pos);		
		Table tinfo = Windows[nItem];
		WDG.RemoveReceiver(tinfo.h);
		delete tinfo.rec;
		Windows.erase( Windows.begin() + nItem);
		TableList.DeleteItem(nItem);						
	}	
}

void MainDlg::OnBnClickedButton3()
{
#ifdef DEBUG
	MainWindow win;
	win.DoModal();
#endif
}

void MainDlg::OnBnClickedButtonEdit()
{
	POSITION pos = TableList.GetFirstSelectedItemPosition();
	if (pos == NULL)
		return;

	while (pos)
	{
		int nItem = TableList.GetNextSelectedItem(pos);

		PositionEditDlg posdlg(NULL, Windows[nItem].pos);
		if(posdlg.DoModal() == IDOK)
		{
			Windows[nItem].rec->SetPos(posdlg.Position-1);			
			Windows[nItem].pos = posdlg.Position;
		}
	}	
}

void MainDlg::OnBnClickedButtonStart()
{
	WDG.Start(1000, false);
}

void MainDlg::OnBnClickedButtonStop()
{
	WDG.Stop();
}
